home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
msn
/
P2P
/
SLPMessage.pyo
(
.txt
)
< prev
Wrap
Python Compiled Bytecode
|
2008-10-13
|
12KB
|
334 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
import logging
log = logging.getLogger('msn.slp.message')
from util import callsback, get
class ParseError(Exception):
pass
class Content(object):
SESSION_REQ = 'application/x-msnmsgr-sessionreqbody'
SESSION_CLOSE = 'application/x-msnmsgr-sessionclosebody'
TRANS_REQ = 'application/x-msnmsgr-transreqbody'
TRANS_RESP = 'application/x-msnmsgr-transrespbody'
class MIMEMessage(object):
def __init__(self, headers = { }, body = '', **moreheaders):
moreheaders.update(headers)
self.headers = moreheaders
self.body = body
def __getitem__(self, key):
return self.headers.__getitem__(key)
def __setitem__(self, key, val):
return self.headers.__setitem__(key, str(val))
def __str__(self):
res = []
headers = getattr(self, 'HEADERS', self.headers)
for key in headers:
if key in self.headers:
res.append('%s: %s' % (key, self.headers[key]))
continue
for key in self.headers:
if key not in headers:
res.append('%s: %s' % (key, self.headers[key]))
continue
res.append('')
res.append(self.body)
return '\r\n'.join(res)
def parse(cls, raw):
headers = { }
body = ''
lines = raw.split('\r\n')
line = lines.pop(0)
while line:
(key, val) = line.split(':', 1)
key = key.strip()
val = val.strip()
headers[key] = val
line = lines.pop(0)
if lines:
body = '\r\n'.join(lines)
return cls(headers, body)
parse = classmethod(parse)
class SLPMessage(MIMEMessage):
class Type(object):
UNKNOWN = 'unknown'
REQUEST = 'req'
RESPONSE = 'resp'
HEADERS = [
'To',
'From',
'Via',
'CSeq',
'Call-ID',
'Max-Forwards']
STATUS_MESSAGE = {
200: '200 OK',
404: '404 Not Found',
500: '500 Internal Error',
603: '603 Decline' }
def __init__(self, *a, **k):
self.method = ''
self.status = -1
MIMEMessage.__init__(self, *a, **k)
def message_type(self):
if self.method:
return self.Type.REQUEST
elif self.status != -1:
return self.Type.RESPONSE
else:
return self.Type.UNKNOWN
message_type = property(message_type)
def clear(self):
MIMEMessage.clear(self)
self.method = ''
self.status = -1
def request(cls, method, to = '', frm = '', branch = '', command_sequence = 0, call_id = '', max_forwards = 0):
self = cls()
self.method = method
self._SLPMessage__build(to, frm, branch, command_sequence, call_id, max_forwards)
return self
request = classmethod(request)
def response(cls, status, to = '', frm = '', branch = '', command_sequence = 0, call_id = '', max_forwards = 0):
self = cls()
self.status = status
int(self.status)
self._SLPMessage__build(to, frm, branch, command_sequence, call_id, max_forwards)
return self
response = classmethod(response)
def response_to(cls, status, msg):
self = cls()
self.status = status
int(self.status)
headers = []
for name in msg.headers:
if name in self.HEADERS:
value = msg.headers[name][:]
if name == 'CSeq':
value = str(int(value) + 1)
headers.append((name, value))
continue
self.headers.update(headers)
self.headers['To'] = msg.headers['From'][:]
self.headers['From'] = msg.headers['To'][:]
return self
response_to = classmethod(response_to)
def add_body(self, body = None):
if body != None:
content_type = body.content_type
body_str = str(body)
body_len = len(body_str)
else:
content_type = 'null'
body_str = ''
body_len = 0
self['Content-Type'] = content_type
self['Content-Length'] = body_len
self.body = body_str
def parse(cls, raw):
if raw.find('MSNSLP/1.0') < 0:
raise ParseError("message doesn't seem to be an MSNSLP/1.0 message", raw)
(start_line, content) = raw.split('\r\n', 1)
start_line = start_line.split(' ')
if not start_line:
return None
try:
if start_line[0] in ('INVITE', 'BYE', 'ACK', '\x00\x00\x00'):
method = start_line[0].strip()
status = -1
else:
method = ''
status = int(start_line[1])
self = MIMEMessage.parse(content)
self.__class__ = cls
self.method = method
self.status = status
log.debug('SLPMessage received: method=%r, status=%r, headers=%r', self.method, self.status, dict(self.headers))
slpbody = SLPBody.parse(self.body, self['Content-Type'])
self.add_body(slpbody)
return self
except Exception:
e = None
import traceback as traceback
traceback.print_exc()
log.info('Bad data in SLPMessage.parse: %r', raw)
return None
parse = classmethod(parse)
def __str__(self):
result = []
if self.method:
result.append('%s MSNMSGR:%s MSNSLP/1.0' % (self.method, self.headers['To'][9:-1]))
else:
result.append('MSNSLP/1.0 %s' % self.STATUS_MESSAGE[self.status])
result.append(MIMEMessage.__str__(self))
return '\r\n'.join(result)
def __build(self, to, frm, branch, command_sequence, call_id, max_forwards):
if to:
self['To'] = '<msnmsgr:%s>' % to
if frm:
self['From'] = '<msnmsgr:%s>' % frm
if branch:
self['Via'] = 'MSNSLP/1.0/TLP ;branch=%s' % branch
self['CSeq'] = str(command_sequence) + ' '
if call_id:
self['Call-ID'] = call_id
self['Max-Forwards'] = max_forwards
class SLPBody(MIMEMessage):
def __init__(self, content_type, *a, **k):
MIMEMessage.__init__(self, *a, **k)
self.content_type = content_type
def parse(cls, raw, content_type):
raw = raw[:-1]
self = MIMEMessage.parse(raw)
self.__class__ = cls
self.content_type = content_type
log.debug('SLPBody received: content_type=%r, headers=%r', self.content_type, dict(self.headers))
return self
parse = classmethod(parse)
def __str__(self):
return MIMEMessage.__str__(self) + '\x00'
class SLPSessionInviteBody(SLPBody):
def __init__(self, *a, **k):
SLPBody.__init__(self, Content.SESSION_REQ, *a, **k)
def request(cls, eufguid, sessionid, chan_state, appid, context, body = ''):
headers = {
'EUF-GUID': eufguid,
'SessionID': sessionid,
'AppID': appid,
'Context': context }
return cls(headers, body)
request = classmethod(request)
def response(cls, sessionid, chan_state, body = ''):
headers = {
'SessionID': sessionid }
return cls(headers, body)
response = classmethod(response)
def response_to(cls, msg):
return cls.response(msg.headers['SessionID'], msg.headers['SChannelState'], '')
response_to = classmethod(response_to)
class SLPTransferInviteBody(SLPBody):
def __init__(self, bridges, netid, conntype, upnp, icf, hashednonce, sessionid, chanstate, body):
headers = { }
headers['Bridges'] = bridges
headers['NetID'] = netid
headers['Conn-Type'] = conntype
headers['UPnPNat'] = upnp
headers['ICF'] = icf
headers['Hashed-Nonce'] = hashednonce
headers['SessionID'] = sessionid
SLPBody.__init__(self, Content.TRANS_REQ, headers, body)
def request(cls, bridges, netid, conntype, upnp, icf, hashednonce, sessionid, chanstate, body):
return cls(bridges, netid, conntype, upnp, icf, hashednonce, sessionid, chanstate, body)
request = classmethod(request)
def response(cls, *a, **k):
raise TypeError('Invite only available as request')
response = classmethod(response)
class SLP_Bye(SLPBody):
def __init__(self):
SLPBody.__init__(self, Content.SESSION_CLOSE)
class SLP_404(SLPMessage):
def response_to(cls, bad_msg):
return SLPMessage.response_to(404, bad_msg)
response_to = classmethod(response_to)
class SLP_500(SLPMessage):
def response_to(cls, bad_msg):
return SLPMessage.response_to(500, bad_msg)
response_to = classmethod(response_to)
def main():
data = 'INVITE MSNMSGR:bob@hotmail.com MSNSLP/1.0\r\nTo: <msnmsgr:bob@hotmail.com>\r\nFrom: <msnmsgr:alice@hotmail.com>\r\nVia: MSNSLP/1.0/TLP ;branch={33517CE4-02FC-4428-B6F4-39927229B722}\r\nCSeq: 0 \r\nCall-ID: {9D79AE57-1BD5-444B-B14E-3FC9BB2B5D58}\r\nMax-Forwards: 0\r\nContent-Type: application/x-msnmsgr-sessionreqbody\r\nContent-Length: 326\r\n\r\nEUF-GUID: {A4268EEC-FEC5-49E5-95C3-F126696BDBF6}\r\nSessionID: 1980589\r\nAppID: 1\r\nContext: PG1zbm9iaiBDcmVhdG9yPSJidWRkeTFAaG90bWFpbC5jb20iIFNpemU9IjI0NTM5IiBUeXBlPSIzIiBMb2NhdGlvbj0iVEZSMkMudG1wIiBGcmllbmRseT0iQUFBPSIgU0hBMUQ9InRyQzhTbEZ4MnNXUXhaTUlCQVdTRW5YYzhvUT0iIFNIQTFDPSJVMzJvNmJvc1p6bHVKcTgyZUF0TXB4NWRJRUk9Ii8+DQoA\r\n\r\n\x00'
if __name__ == '__main__':
main()